import router from "@src/router";
import {storeBindingsBehavior} from "mobx-miniprogram-bindings";
import base from "@src/store/base";
import mall from "@src/store/mall";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeTitleColor', 'themeColor'],
  },{
    store: mall,
    actions: ['setAddress']
  },],
  properties: {
    store: {
      type: Object,
      value: {}
    },
    distance: {
      type: Number,
      value: null
    },
    address: {
      type: Object,
      value: null
    },
    deliveryType: {
      type: String
    }
  },
  data: {},
  methods: {
    handleToStores() {
      router.push({
            'name': 'mall/service-stores',
            params: {deliveryType: this.data.deliveryType, showTabs: false, activeTab: 'store'},
            events: {
              'changeStore': (data) => {
                // 这里只是修改店铺
                this.triggerEvent("store", data)
              },
            }
          });
    },
    handleToAddress() {
      router.push({
            'name': 'mall/service-stores',
            params: {deliveryType: this.data.deliveryType, showTabs: false, activeTab: 'address'},
            events: {
              'changeAddress': (data) => {
                this.setAddress(data.address)
                this.updateStoreBindings()
              }
            }
      });
    },
    handleSwitchType(e) {
      let deliveryType = ''
      let activeTab = ''
      if (this.data.deliveryType === 'LOCAL') {
        deliveryType = 'SELF_PICKUP'
        activeTab = 'store'
      } else {
        deliveryType = 'LOCAL'
        activeTab = 'address'
      }
      console.info(deliveryType)
      console.info(this.data.deliveryType)
      router.push(
          {
            'name': 'mall/service-stores',
            params: {deliveryType: deliveryType, showTabs: true, activeTab: activeTab},
            events: {
              'changeStore': (data) => {
                // 这里只是修改店铺
                this.triggerEvent("store", data)
              },
              'changeAddress': (data) => {
                this.setAddress(data.address)
                this.updateStoreBindings()
                this.triggerEvent("store", {deliveryType: 'LOCAL'})
              }
            }
          });
    },
  }
});
